import { Controller, Get, Query } from '@nestjs/common';
import { CalculatePointTowerService } from './calculate-point-tower.service';
import { CalculatePointAcrossService } from './calculate-point-across.service';
import { CalculateZoneService } from './calculate-zone.service';

@Controller('calculate')
export class CalculateController {
  constructor(
    private readonly calculatePointTowerService: CalculatePointTowerService,
    private readonly calculatePointAcrossService: CalculatePointAcrossService,
    private readonly calculateZoneService: CalculateZoneService,
  ) {}

  @Get()
  async calculate(@Query() args: Record<string, any> = {}) {
    const { zoneId } = args;

    const lstPointTowers =
      await this.calculatePointTowerService.calculate(args);
    const xyTowers = lstPointTowers.map((t) => ({ x: t.calcX, y: t.calcY }));

    const lstPointAcorss =
      await this.calculatePointAcrossService.calculate(args);
    const xyAcrosss = lstPointAcorss.map((t) => ({ x: t.calcX, y: t.calcY }));

    const xys = [...xyTowers, ...xyAcrosss];
    await this.calculateZoneService.calculate(xys, +zoneId);

    return 'ok';
  }
}
